<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card v-loading="isLoading">
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addJurisdiction(1, '0')"
            >添加权限</el-button
          >
        </div>
        <el-table border :data="treePowerList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.type === 1"
                size="small"
                type="text"
                @click="addJurisdiction('2', row.id)"
                >添加权限点</el-button
              >
              <el-button size="small" type="text" @click="checkJurisdiction(row.id)"
                >查看</el-button
              >
              <el-button size="small" type="text" @click="delJurisdiction(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog
        :visible="showDialog"
        :title="showTitle"
        :close-on-click-modal="false"
        @close="closeWindows"
      >
        <!-- 表单内容 -->
        <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0" />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right">
            <el-button @click="closeWindows">取消</el-button>
            <el-button type="primary" @click="submitJurisdiction">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  jurisdictionListApi,
  addJurisdictionApi,
  delJurisdictionApi,
  checkJurisdictionApi,
  alterJurisdictionApi
} from '@/api/permission'
import { findLeader } from '@/utils/index'

export default {
  name: 'Employess',
  data() {
    return {
      // 权限列表
      powerList: [],
      // 树形权限列表
      treePowerList: [],
      // 控制弹窗
      showDialog: false,
      isLoading: false,
      // 添加表单
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不可为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不可为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不可为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showTitle() {
      if (this.formData.id) {
        return '查看编辑'
      } else {
        return '添加权限'
      }
    }
  },
  created() {
    this.jurisdictionList()
  },
  methods: {
    // 获取权限点列表
    async jurisdictionList() {
      this.isLoading = true
      const { data } = await jurisdictionListApi()
      console.log(data)
      this.powerList = data.data // 没被转换树形结构的数组
      console.log(this.powerList)
      this.transformTree()
      this.isLoading = false
    },
    // 封装转换树形结构
    transformTree() {
      this.treePowerList = findLeader(this.powerList, '0')
    },
    // 显示弹框
    addJurisdiction(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 点击确定提交权限
    async submitJurisdiction() {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          // 修改
          console.log(this.formData)
          alterJurisdictionApi(this.formData)
            .then(res => {
              this.$message.success('修改成功')
              // 关闭弹窗
              this.showDialog = false
              // 刷新页面
              this.jurisdictionList()
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          // 添加
          addJurisdictionApi(this.formData)
            .then(res => {
              this.$message.success('添加成功')
              // 关闭弹窗
              this.showDialog = false
              // 刷新页面
              this.jurisdictionList()
            })
            .catch(err => {
              console.log(err)
            })
        }
      })
    },
    // 删除权限
    delJurisdiction(id) {
      this.$confirm('亲！您确定要删除吗？', '温馨提示').then(async () => {
        delJurisdictionApi(id).then(res => {
          this.$message.success('删除成功')
          // 刷新页面
          this.jurisdictionList()
        })
      })
    },
    // 关闭弹窗
    closeWindows() {
      this.showDialog = false
      this.$refs.ruleForm.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 数据回填
    async checkJurisdiction(id) {
      this.showDialog = true
      const { data } = await checkJurisdictionApi(id)
      this.formData.id = id
      this.formData = data.data
    }
  }
}
</script>

<style></style>
